import { reactive, ref } from "vue";
import { getDeptAndUserApi } from "@/api/approval/approvalManagement";

export default function InitHook() {
  //表单数据
  const formState = reactive<ApprovalManagement.editForm>({
    type: "",
    remark: "",
    dateType: undefined,
    dates: 0,
    finCloseCase: 0,
    bizConfirmation: 1,
    isCreateReceivable: 0,
    companyProportion: 0,
    bizProportion: 0,
    approvalList: [
      {
        approvalType: undefined,
        deptId: undefined,
        userIdList: []
      },
      {
        approvalType: undefined,
        deptId: undefined,
        userIdList: []
      }
    ]
  });

  const tableData = reactive([]); //表格数据

  const loading = ref<boolean>(false); //等待状态
  const getDeptOpts = ref<any>([]); //获取部门

  //查询部门及部门用户
  const getDeptAndUserHandle = () => {
    loading.value = true;
    return getDeptAndUserApi()
      .then((res: any) => {
        const { data } = res;
        getDeptOpts.value = data.map((t: any) => {
          return { name: t.deptName, id: t.deptId, ...t };
        });
      })
      .finally(() => {
        loading.value = false;
      });
  };

  //表单ref
  const formRef = ref(null);
  return {
    tableData,
    formState,
    loading,
    formRef,
    getDeptAndUserHandle,
    getDeptOpts
  };
}
